# -*- coding:utf-8 -*-
"""
作者：ChenTao
日期：2023年03月09日
"""
import multiprocessing
import numpy as np
import cv2
import argparse


def Record_Video(VideoName, index, imgsz, fps, path, show):
    video_name = f'{path}/{VideoName}.avi'

    cap = cv2.VideoCapture(index)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, imgsz[0])
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, imgsz[1])
    cap.set(cv2.CAP_PROP_FPS, fps)

    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    out = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc('M','J','P','G'), fps, imgsz)

    i = 1
    try:
        while (cap.isOpened()):
            ret, frame = cap.read()
            if ret == True:
                frame = cv2.flip(frame, 1)
                i += 1
                #        out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))   将视频转换为灰色的源
                out.write(frame)
                print(f'{multiprocessing.current_process().name}--写入第--{i}--帧，如果要退出，请按1')

                if show:
                    cv2.imshow('frame', frame)
                if cv2.waitKey(1) & 0xFF == ord('1'):
                    break
            else:
                break
    except KeyboardInterrupt:
        cap.release()
        out.release()
        cv2.destroyAllWindows()


if __name__ == "__main__":
    parse = argparse.ArgumentParser()
    parse.add_argument('--VideoName', default='pvc_67', help='video name')
    parse.add_argument('--cap', default=1, help='video index')
    parse.add_argument('--imgsz', type=list, default=[1920, 1080])
    parse.add_argument('--fps', type=int, default=30)
    parse.add_argument('--path', type=str, default='video')
    parse.add_argument('--show', type=bool, default=True)
    opt = parse.parse_args()
    Record_Video(opt.VideoName, opt.cap, opt.imgsz, opt.fps, opt.path, opt.show)
